package com.aiti.lulian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.lulian.bo.LoginUserInfo;
import com.aiti.lulian.dto.industry.IndustrySupportParamDto;
import com.aiti.lulian.entity.industry.CardGovIndustrySupport;
import com.aiti.lulian.mapper.card.industry.CardGovIndustrySupportMapper;
import com.aiti.lulian.service.CardGovIndustrySupportService;
import com.aiti.lulian.vo.industry.IndustrySupportVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description: CardGovIndustrySupportServiceImpl
 * @auth fcb
 * @date 2025-03-04 18:06
 **/
@Service
@Slf4j
public class CardGovIndustrySupportServiceImpl extends BaseSerivceImpl<CardGovIndustrySupportMapper, CardGovIndustrySupport> implements CardGovIndustrySupportService {

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void addIndustrySupport(IndustrySupportParamDto paramDto, LoginUserInfo loginUserInfo) {
        // 1. 参数校验
        this.checkParams(paramDto);
        // 2. 查询当前产业下，该名称功能是否已经存在
        Integer count = this.baseMapper.getCount(paramDto.getSupportName(), paramDto.getIndustryId());
        if(count > 0) {
            throw new BusinessException("该产业下，该名称功能已经存在");
        }
        // 3. 新增
        CardGovIndustrySupport industrySupport = new CardGovIndustrySupport();
        BeanUtil.copyProperties(paramDto, industrySupport);
        industrySupport.setBaseId(UUID.randomUUID().toString());
        industrySupport.setBaseCreateBy(loginUserInfo.getUserId());

        this.save(industrySupport);
    }

    /**
     * 修改产业支持功能
     * @param paramDto
     * @param loginUserInfo
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void updateIndustrySupport(IndustrySupportParamDto paramDto, LoginUserInfo loginUserInfo){
        if(StrUtil.isEmpty(paramDto.getBaseId())) {
            throw new BusinessException("参数错误");
        }
        CardGovIndustrySupport po = new CardGovIndustrySupport();
        BeanUtil.copyProperties(paramDto, po);
        po.setBaseUpdateBy(loginUserInfo.getUserId());
        this.baseMapper.updateIndustrySupport(po);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void deleteIndustrySupport(List<String> baseIds) {
        if(CollectionUtil.isEmpty(baseIds)) {
            return;
        }
        // 判断这些产业功能下，是否还有产业功能的内容，如有，则需先删除内容
        Integer contentCount = this.baseMapper.queryCountSupportContent(baseIds);
        if(contentCount > 0) {
            throw new BusinessException("要删除的产业功能支持下，还有产业功能的内容，请先删除");
        }
        this.baseMapper.deleteIndustrySupport(baseIds);
    }

    /**
     * 查询当前产业下所有的产业功能
     * @param industryId 产业id
     * @return
     */
    @Override
    public List<IndustrySupportVo> getIndustrySupportList(String industryId) {
        if(StrUtil.isEmpty(industryId)) {
            throw new BusinessException("参数错误");
        }
        return this.baseMapper.queryIndustrySupportList(industryId);
    }

    /**
     * 根据主键查询产业功能
     * @param baseId
     * @return
     */
    public IndustrySupportVo getIndustrySupportById(String baseId) {
        if(StrUtil.isEmpty(baseId)) {
            throw new BusinessException("参数错误");
        }
        CardGovIndustrySupport cardGovIndustrySupport = this.baseMapper.selectById(baseId);
        if(cardGovIndustrySupport == null || cardGovIndustrySupport.getIsDelete() == 1) {
            throw new BusinessException("数据不存在");
        }
        IndustrySupportVo vo = new IndustrySupportVo();
        BeanUtil.copyProperties(cardGovIndustrySupport, vo);
        return vo;
    }

    private void checkParams(IndustrySupportParamDto paramDto) {
        if(StrUtil.isEmpty(paramDto.getSupportName())) {
            throw new BusinessException("产业支持功能名称不能为空");
        }
        if(StrUtil.isEmpty(paramDto.getIndustryId())) {
            throw new BusinessException("产业支持功能所属产业不能为空");
        }
    }
}
